﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HR60_PanelSoft.Model;
using HR60_PanelSoft.Model.BaseModel;
using HR60_PanelSoft.View.Base;

namespace HR60_PanelSoft.View.TabForms.ListItems
{
    //GC: [g1]修正场景编辑不保存到内存 [SceneDeviceItem]@s2
    //GC: [g2]修正场景编辑不加载[SceneDeviceItem]@s3
    //GC: [g3]修正外部信号不保存[SceneDeviceItem] @s1
    //GC: [g4]修正SceneEvent.attrs填充内容[SceneDeviceItem]
    //GC: [g5]bug#3089[SceneDeviceItem]
	/// <summary>
	/// 单控设备场景控制编辑
	/// </summary>
	public partial class SceneDeviceItem : BaseFormControl
	{
		Device dev = null;

		public SceneDeviceItem(Device dev)
		{
			InitializeComponent();

			this.dev = dev;
			cbEnabled.Checked = false;
			txtName.Text = dev.nickName;
			selValue.bindData(dev.type.attrs[0].values);

			//selValue.ValueChanged += new EventHandler(valueChange);
            
			//cbEnabled.CheckedChanged += new EventHandler(valueChange);
            selValue.ValueChanged += new ValueChangedHandler(valueChange);
            cbEnabled.CheckedChanged += new EventHandler(valueChange_Normal);
		}

		// 目标设备
		public Device TagDevice
		{
			get
			{
				return dev;
			}
		}


        /* g1.s2 传递ComboBox选择项 */
        public SelectItem SelectedItem
        {
            get
            {
                return selValue.SelectedItem;
            }
        }
        /* end of g1.s2  */

        /* g3.s1 */
        /* 获取选中状态 */
        public bool Checked
        {
            get
            {
                return cbEnabled.Checked;
            }
        }
        /* end of g3.s1 */
		// 设置开
		public void open()
		{
			cbEnabled.Checked = true;
            /* [g5.s1] */
			//selValue.Value = dev.type.attrs[0].values["开"];
            if (dev.type.attrs[0].values.ContainsValue("开"))
            {
                selValue.Value = dev.type.attrs[0].values["开"];
            }
            else
            {
                selValue.Value = dev.type.attrs[0].values.Last().Value;
            }

            /* end of [g5.s1] */
		}

		// 设置关
		public void close()
		{
			cbEnabled.Checked = true;
            /* [g5.s2] */
			//selValue.Value = dev.type.attrs[0].values["关"];
            if (dev.type.attrs[0].values.ContainsValue("关"))
            {
                selValue.Value = dev.type.attrs[0].values["关"];
            }
            else
            {
                selValue.Value = dev.type.attrs[0].values.First().Value;
            }
            /* end of [g5.s2] */
		}

		// 重写Value属性，返回SceneEvent对象
		public override object Value
		{
			get
			{
				if (cbEnabled.Checked)
				{
					SceneEvent se = new SceneEvent();
					se.device = dev.uuid;
                    /* g1.s2 此处有bug */
					//se.attrValueKeys.Add(selValue.Text);
                    //se.attrs.Add(dev.type.attrs[0].name);
                    /* [g4.s4] */
                    //se.attrs.Add(selValue.SelectedItem.text);
                    se.attrs.Add(dev.type.attrs[0].name);
                    /* end of [g4.s4] */
                    se.attrValueKeys.Add((string)selValue.SelectedItem.value);
                    /* end of g1.s2 */
					return se;
				}
				else
				{
					return null;
				}
			}
			set
			{
				if (value == null)
				{
					cbEnabled.Checked = false;
                    /* g2.s3 处理Text为空 */
                    selValue.Text = ((SelectItem)selValue.Items[0]).text;
                    /* end of g2.s3 */
				}
				else
				{
					SceneEvent se = (SceneEvent)value;
                    /* g2.s2 此处有bug */
					//selValue.Text = se.attrValueKeys[0]; 
                    /* [g4.s3] */
                    //selValue.Text = se.attrs[0];    /* 为何使用0位置的值？-->负载设备只有一个可控属性 */
                    selValue.Value = se.attrValueKeys[0];
                    selValue.Text = se.attrValueKeys[0] == "1" ? "开" : "关";

                    /* end of [g4.s3] */
                    cbEnabled.Checked = true;
                    /* end of g2.s2 */
                    ///* s1 */
                    ////TODO:GC
                    //if (se.device == dev.uuid)
                    //{
                    //    int i = se.attrs.FindIndex(x => x == selValue.Text);
                    //    if (i < -1 || i >= se.attrs.Count) 
                    //        return;
                    //    selValue.Value = se.attrValueKeys[i];
                    //    cbEnabled.Checked = true;
                    //}
                    ///* end of s1 */
				}
			}
		}

        /* g2.s1 将内存对象加载到控件 */
        /// <summary>
        /// 将内存对象加载到控件
        /// </summary>
        /// <param name="se"></param>
        public void LoadValue(SceneEvent se)
        {
            if (se.device != dev.uuid)
                return;
            if (se.attrs.Count > 0 || se.attrValueKeys.Count > 0)
            {
                /* [g4.s2] */
                //selValue.Text = se.attrs[0]; /* 负载设备只有一个可控属性 */
                selValue.Value = se.attrValueKeys[0];
                selValue.Text = se.attrValueKeys[0] == "1" ? "开" : "关";
                /* end of [g4.s2] */

                cbEnabled.Checked = true;
            }
            else
            {
                cbEnabled.Checked = false;
            }

        }

        /* end of g2.s1 */

        /* g1.s1 保存场景事件到内存对象 */
        /// <summary>
        /// 保存场景事件到内存对象
        /// </summary>
        /// <param name="to">目的数据</param>
        public void SaveValue(Scene to)
        {
            SceneEvent se = new SceneEvent();
            se.device = dev.uuid;
            /* [g4.s1]修正内容填充 */
            //se.attrs.Add(selValue.SelectedItem.text);
            se.attrs.Add(dev.type.attrs[0].name);
            /* end of [g4.s1] */
            se.attrValueKeys.Add((string)selValue.SelectedItem.value);

            if (to.events.Exists(x => x.device == dev.uuid))  /* 如果存在先删除 */
            {
                SceneEvent temp = to.events.Find(x => x.device == dev.uuid);
                if (null == temp)
                    //GCERR:
                    return;
                to.events.Remove(temp);
            }
            if (cbEnabled.Checked)  /* 只有选中才保存 */
            {
                to.events.Add(se);
            }
        }
        /* end of g1.s1 */

        //GC: 传递ComboBox事件到FormComboBox。SceneDeviceItem @s1
        /* s1 */
        public void addDropDownEvent(EventHandler eh)
        {
            selValue.addDropDownEvent(eh);
        }
        /* end of s1 */
	}
}
